from DrissionPage import Chromium,ChromiumOptions  # 自动化模块
from DrissionPage.common import Actions # 导入动作链
from DrissionPage.common import Keys    # 导入键盘
from pypinyin import pinyin,Style # 导入拼音模块

def yuyan(chinese):
    zw = pinyin(chinese,style=Style.NORMAL)
    string = "".join(t[0] for t in zw)
    return string

def buy(Go_city,To_city,Sj,Page_num):



    # 自动打开浏览器
    eo = ChromiumOptions()
    eo = ChromiumOptions().set_browser_path('C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe')

    zdh = Chromium(addr_or_opts=eo)
    zdh = zdh.latest_tab
    dzl = Actions(zdh)
    zdh.get('https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc&fs=%E5%B9%BF%E5%B7%9E,GZQ&ts=%E4%B8%8A%E6%B5%B7,SHH&date=2025-05-13&flag=N,N,Y')

    dzl.move_to('css:#fromStationText').click().type(yuyan(Go_city)) # 定位出发地输入框元素
    zdh.ele('css:#fromStationText').input(Keys.ENTER) # 输入出发地

    dzl.move_to('css:#toStationText').click().type(yuyan(To_city)) # 定位目的地输入框元素
    zdh.ele('css:#toStationText').input(Keys.ENTER) # 输入目的地

    zdh.ele('css:#train_date').clear()
    zdh.ele('css:#train_date').input(Sj) # 定位出发时间输入框元素

    zdh.ele('css:#query_ticket').click() # 点击查询按钮

    zdh.ele(f'css:#queryLeftTable tr:nth-child({int(Page_num)*2-1}) .btn72').click() #预定车票的位置

    #判断是否登录
    login = zdh.ele('css:#login_user').text
    if login =='登录':

        # 登录
        zdh.ele('css:#J-userName').input() # 输入用户名(在括号内输入)
        zdh.ele('css:#J-password').input() # 输入密码(在括号内输入)
        zdh.ele('css:#J-login').click() # 点击登录
        zdh.ele('css:#id_card').input() # 输入身份证后四位(在括号内输入)
        zdh.ele('css:#verification_code').click() # 点击发送验证码
        yzm = int(input('请输入验证码：')) # 输入验证码
        zdh.ele('css:#code').input(yzm) # 输入验证码
        zdh.ele('css:#sureClick').click() # 点击确定
    else:
        print('已登录')



import json

import requests

k = open('city.json',encoding = 'utf-8').read()
city = json.loads(k)
go_city = input("请输入出发城市：")
to_city = input("请输入到达城市：")
sj = input("请输入出发时间：")

url = f"https://kyfw.12306.cn/otn/leftTicket/queryG?leftTicketDTO.train_date={sj}&leftTicketDTO.from_station={city[go_city]}&leftTicketDTO.to_station={city[to_city]}&purpose_codes=ADULT"


headers = {
    "user-agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0",
    "cookie ": "_uab_collina=174646404451669250709201; JSESSIONID=EAA7012F65A576F6BB59250F53D3FC79; guidesStatus=off; highContrastMode=defaltMode; cursorStatus=off; _jc_save_fromStation=%u5E7F%u5DDE%2CGZQ; _jc_save_toStation=%u4E0A%u6D77%2CSHH; _jc_save_wfdc_flag=dc; route=495c805987d0f5c8c84b14f60212447d; BIGipServerotn=1490616586.24610.0000; _jc_save_toDate=2025-05-12; BIGipServerpassport=870842634.50215.0000; _jc_save_fromDate=2025-05-12",
    "referer" : "https://kyfw.12306.cn/otn/leftTicket/init?"
}

res = requests.get(url, headers=headers)

JSON = res.json()
data = JSON["data"]["result"]
page_num = 0
for i in data:
    index = i.split("|")
    page_num += 1
    che_ci = index[3] #车次
    go_time = index[8] #始发时间
    top_time = index[9] #到达时间
    time = index[10] #历时
    nb_vip = index[32] #商务
    vip = index[31] #一等
    pt = index[30] #二等

    dit = {
        '序号':page_num,
        '车次':che_ci,
        '出发时间':go_time,
        '到达时间':top_time,
        '历时':time,
        '商务座':nb_vip,
        '一等座':vip,
        '二等座':pt


    }
    print(dit)
page_num = input("请输入对应的车序号：")
buy(Go_city=go_city,To_city=to_city,Sj=sj,Page_num=page_num)
